package com.leave.servlet;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.leave.entity.Apply;
import com.leave.entity.User;
import com.leave.service.ApplyService;
import com.leave.util.BaseServlet;

public class ApplyServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;
	private ApplyService applyService = new ApplyService();

	public String Leave_Apply(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
		Apply apply = new Apply();
		// 获取输入框中的文字
		String l_s_card = req.getParameter("l_s_card");
		String s_username = req.getParameter("s_username");
		String g_name = req.getParameter("g_name");
		String c_name = req.getParameter("c_name");
		String s_phone = req.getParameter("s_phone");
		String l_begintime = req.getParameter("l_begintime");
		String l_endtime = req.getParameter("l_endtime");
		String l_address = req.getParameter("l_address");
		String l_cause = req.getParameter("l_cause");
		apply.setL_s_card(l_s_card);
		apply.setS_username(s_username);
		apply.setG_name(g_name);
		apply.setC_name(c_name);
		apply.setS_phone(s_phone);
		apply.setL_begintime(l_begintime);
		apply.setL_endtime(l_endtime);
		apply.setL_address(l_address);
		apply.setL_cause(l_cause);

		applyService.add(apply);

		return "/WEB-INF/jsp/student/student.jsp";

	}

	public String Show_Apply_Record(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
		User user = (User) req.getSession().getAttribute("user");
		String account = user.getAccount();
		List<Apply> apply = applyService.show(account);
		// 将获取到的记录保存到session中
		req.getSession().setAttribute("applylist", apply);
		return "/WEB-INF/jsp/student/leave_record.jsp";

	}

	public String Show_Apply_Detail(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
		String aid = req.getParameter("aid");
		Apply apply = applyService.show_apply(Integer.parseInt(aid));
		req.getSession().setAttribute("apply", apply);
		return "/WEB-INF/jsp/student/leave_show.jsp";

	}

	public String Show_Apply_Update_Detail(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
		String aid = req.getParameter("aid");
		Apply apply = applyService.show_apply(Integer.parseInt(aid));
		req.getSession().setAttribute("apply", apply);
		return "/WEB-INF/jsp/student/leave_update.jsp";

	}

	public String Update_Apply_Detail(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
		Apply apply = new Apply();
		String aid = req.getParameter("aid");
		System.out.println(aid);
		String g_name = req.getParameter("g_name");
		String c_name = req.getParameter("c_name");
		String s_phone = req.getParameter("s_phone");
		String l_begintime = req.getParameter("l_begintime");
		String l_endtime = req.getParameter("l_endtime");
		String l_address = req.getParameter("l_address");
		String l_cause = req.getParameter("l_cause");
		apply.setG_name(g_name);
		apply.setC_name(c_name);
		apply.setS_phone(s_phone);
		apply.setL_begintime(l_begintime);
		apply.setL_endtime(l_endtime);
		apply.setL_address(l_address);
		apply.setL_cause(l_cause);
		apply.setAid(Integer.parseInt(aid));

		applyService.update_apply(apply);
		return "/WEB-INF/jsp/student/student.jsp";

	}

	public String Delete_Apply(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String aid = req.getParameter("aid");
		applyService.delete_apply(Integer.parseInt(aid));
		return "/WEB-INF/jsp/student/student.jsp";
	}
	
	public String Show_Leave_Cancle(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		String account = user.getAccount();
		List<Apply> apply_cancle = (List<Apply>) applyService.show_cancle(3,account);//3表未销假
		req.getSession().setAttribute("apply_cancle", apply_cancle);
		return "/WEB-INF/jsp/student/leave_cancle.jsp";
	}
	
	public String Leave_Cancle(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String aid = req.getParameter("aid");
		applyService.leave_cancle(4, Integer.parseInt(aid));//4表示为已销假
		return "/WEB-INF/jsp/student/student.jsp";
	}
	
	

}
